# -*- coding: utf-8 -*-
from . import BaseController
from backend.libs import myjson as json
from backend.models import Serie, Recommend, Video
from datetime import datetime


class List(BaseController):

    def get(self):
        recommend = Recommend.objects
        state = self.int_argument('state', 0)
        recommend = recommend.filter(state=state)
        recommend = recommend.order_by('show_order').select_related('video', 'serie')
        number_per_page = self.int_argument('pageSize', 50)
        paginator = self.pagination(recommend.all(), number_per_page=number_per_page)
        return self.render_json(data=paginator)


class Detail(BaseController):

    def get(self):
        id = self.int_argument('id', 0)
        if id <= 0:
            return self.render_json(code=1001, msg='参数错误')
        recommend = Recommend.objects.filter(id=id).first()
        
        return self.render_json(data=recommend.to_dict())


class ListAll(BaseController):

    def get(self):
        elements = Recommend.objects.filter(state=0).order_by('show_order').all()
        data = [ {'label': e.name, 'value': e.id} for e in elements ]
        return self.render_json(data={'elements': data})


class BaseRecommendController(BaseController):

    def set_recommend_properties(self, recommend, data, opt_type=0):
        vid = data.get('video_id')
        sid = data.get('serie_id')
        if not vid and not sid:
            return None
        video = None
        serie = None
        if vid:
            video = Video.objects.filter(id=vid).first()
        if sid:
            serie = Serie.objects.filter(id=sid).first()
        recommend.show_order = data.get('show_order')
        recommend.state = data.get('state')
        if opt_type == 1:
            recommend.create_time = datetime.now()
        recommend.img_url = data.get('img_url')
        recommend.serie = serie
        recommend.video = video
        recommend.name = data.get('name', '')
        recommend.type = data.get('type')
        recommend.save()
        return recommend

class CreateRecommend(BaseRecommendController):

    def post(self):
        data = json.loads(self.request.body)
        recommend = Recommend()
        recommend = self.set_recommend_properties(recommend, data, opt_type=1)
        if not recommend:
            return self.render_json(code=1001, msg='推荐必须配置一个视频或一个系列')
        return self.render_json(data=recommend.to_dict())


class RecommendDelController(BaseRecommendController):

    def post(self, id):
        recommend = Recommend.objects.filter(id=id).first()
        if not recommend:
            return self.render_json(code=1001, msg='系列不存在')
        recommend.state = 1
        recommend.save()
        return self.render_json(msg='删除成功')


class UpdateRecommend(BaseRecommendController):

    def post(self):
        data = json.loads(self.request.body)
        recommend_id = data.get('id', -1)
        if recommend_id <= 0:
            return self.render_json(code=1001, msg='系列ID不能为空')
        recommend = Recommend.objects.filter(id=recommend_id).first()
        if not recommend:
            return self.render_json(code=1001, msg='系列不存在')
        recommend = self.set_recommend_properties(recommend, data)
        if not recommend:
            return self.render_json(code=1001, msg='推荐必须配置一个视频或一个系列')
        
        return self.render_json(data=recommend.to_dict())